home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 32 / Mac Magazin and MacEasy Magazine CD - Issue 32.iso / Multimedia / PlayerPRO 4.5.5 Dev.Kit / Plug-Ins / Sound Filters Plugs / Normalize.c < prev    next >
C/C++ Source or Header  |  1995-10-08  |  2KB  |  104 lines

  1. /*    Normalize        */
  2. /*    v 0.2            */
  3. /*    1995 by Liane    */
  4.  
  5. //    Usage:
  6. //    Another version of "Maximize", but this one
  7. //    will try to get the maximum gain.
  8. //    Works on the selected part or all the waveform
  9. //    if there is no selection.
  10.  
  11. #include "MAD.h"
  12. #include "PPPlug.h"
  13.  
  14. #if defined(powerc) || defined(__powerc)
  15. enum {
  16.         PlayerPROPlug = kCStackBased
  17.         | RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
  18.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( sData*)))
  19.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( long)))
  20.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( long)))
  21.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoPlug*)))
  22. };
  23.  
  24. ProcInfoType __procinfo = PlayerPROPlug;
  25. #else
  26. #include <A4Stuff.h>
  27. #endif
  28.  
  29.  
  30. #define max(a,b) (((a) > (b)) ? (a) : (b))
  31. #define labs(a) (((a) < 0) ? (-a) : (a))
  32.  
  33. OSErr main(     sData                    *theData,
  34.                 long                    SelectionStart,
  35.                 long                    SelectionEnd,
  36.                 PPInfoPlug                *thePPInfoPlug)
  37. {
  38. long    i, peak = 0, temp;
  39.  
  40.     if (SelectionStart == SelectionEnd) {
  41.         SelectionStart = 0;
  42.         SelectionEnd = theData->size;
  43.     }
  44.  
  45.     switch( theData->amp)
  46.     {
  47.         case 8:
  48.         {
  49.             Ptr    SamplePtr = (theData->data) + SelectionStart;
  50.             for( i = 0; i < SelectionEnd - SelectionStart; i++)
  51.             {
  52.                 temp = *SamplePtr++;
  53.  
  54.                 peak = max (peak, labs(temp));
  55.             }
  56.             
  57.             if( peak != 0)
  58.             {
  59.                 peak = ((long)0x80 * 0x10000) / peak;
  60.                 
  61.                 SamplePtr = (theData->data) + SelectionStart;
  62.                 for( i = 0; i < SelectionEnd - SelectionStart; i++)
  63.                 {
  64.                     temp = *SamplePtr;
  65.             
  66.                     temp = (peak * temp) / 0x10000;
  67.             
  68.                     if( temp > 127) temp = 127;
  69.                     else if( temp < -127 ) temp = -127;
  70.                     
  71.                     *SamplePtr++ = temp;
  72.                 }
  73.             }
  74.         } break;
  75.  
  76.         case 16:
  77.         {
  78.             short    *SamplePtr = (short*) theData->data + (SelectionStart / 2);
  79.  
  80.             for( i = 0; i < (SelectionEnd - SelectionStart) / 2; i++)
  81.             {
  82.                 temp = (long)*SamplePtr++;
  83.                 peak = max (peak, labs(temp));
  84.             }
  85.             
  86.             if( peak != 0)
  87.             {
  88.                 peak = ((long)0x8000 * 0x10000) / peak;
  89.                 
  90.                 SamplePtr = (short*) theData->data + (SelectionStart / 2);
  91.                 for( i = 0; i < (SelectionEnd - SelectionStart) / 2; i++)
  92.                 {
  93.                     temp = (long)*SamplePtr;
  94.             
  95.                     temp = -(peak * temp) / 0x10000;
  96.             
  97.                     *SamplePtr++ = temp;
  98.                 }
  99.             }
  100.         } break;
  101.     }
  102.     
  103.     return noErr;
  104. }